package com.uchiha.nova.core.tool.support;

import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.HeaderConstant;
import com.uchiha.nova.core.tool.util.WebUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;



/**
 * @author 周林静
 * @Date 2021/4/30 10:04
 * @Version 1.0
 */
@Slf4j
public class BaseController  {
    public BaseController() {
    }


    protected NovaUser getNovaUser() {
        HttpServletRequest request = WebUtil.getRequest();
        if (request == null) {
            return null;
        }
        NovaUser novaUser = new NovaUser();
        novaUser.setUserId(Long.valueOf(request.getHeader(HeaderConstant.NOVA_USER_ID)));
        novaUser.setTenantId(request.getHeader(HeaderConstant.NOVA_USER_TENANT));
        novaUser.setRoleName(request.getHeader(HeaderConstant.NOVA_USER_ROLE));
        novaUser.setCompanyId(request.getHeader(HeaderConstant.NOVA_USER_COMPANY));
        novaUser.setUserName(request.getHeader(HeaderConstant.NOVA_USERNAME));
        novaUser.setDeptId(request.getHeader(HeaderConstant.NOVA_USER_DEPT));
        novaUser.setPostId(request.getHeader(HeaderConstant.NOVA_USER_POST));
        novaUser.setDataAuth(request.getHeader(HeaderConstant.NOVA_DATA_AUTH));
        return novaUser;
    }

    /** ============================     API_RESULT    =================================================  */

    /**
     * 返回ApiResult
     *
     * @param data
     * @return R
     */
    public <T> R<T> data(T data) {
        return R.data(data);
    }

    /**
     * 返回ApiResult
     *
     * @param data
     * @param message
     * @return R
     */
    public <T> R<T> data(T data, String message) {
        return R.data(data, message);
    }

    /**
     * 返回ApiResult
     *
     * @param data
     * @param message
     * @param code
     * @return R
     */
    public <T> R<T> data(T data, String message, int code) {
        return R.data(code, data, message);
    }

    /**
     * 返回ApiResult
     *
     * @param message
     * @return R
     */
    public R success(String message) {
        return R.success(message);
    }

    /**
     * 返回ApiResult
     *
     * @param message
     * @return R
     */
    public R fail(String message) {
        return R.fail(message);
    }

    /**
     * 返回ApiResult
     *
     * @param flag
     * @return R
     */
    public R status(boolean flag) {
        return R.status(flag);
    }
}
